<script setup lang="ts" name="PanelExtension">
import { mapState } from 'pinia'
import { useModelerStore } from '@/store/bpmnProcess/modelerStore'
import { getExtensionProperties, removeExtensionProperty } from '../../utils/extension'
import EventEmitter from '@/utils/EventEmitter'
import useMessageBox from '@/hooks/useMessageBox'
import ExtensionPropertyAddEdit from './components/ExtensionPropertyAddEdit.vue'

const { messageBox } = useMessageBox()

const ExtensionPropertyAddEditRef = ref(null)

const extensionsRaw = ref([])
const extensions = ref([])

const mapStates = computed(() => {
  return mapState(useModelerStore, ['getActive'])
})

const add = () => {
  ExtensionPropertyAddEditRef.value.showDialog()
}

const edit = (row, idx) => {
  const res = {
    row: row,
    index: idx
  }
  ExtensionPropertyAddEditRef.value.showDialog(res)
}

const deleteRow = (index) => {
  const msg = '确认<span>删除</span>该数据吗？'
  const msgTips = ''
  messageBox('danger', true, msg, msgTips, () => {
    removeExtensionProperty(mapStates.value.getActive(), extensionsRaw.value[index])
  })
}

const initData = () => {
  extensionsRaw.value = markRaw(getExtensionProperties(mapStates.value.getActive()))
  extensions.value = JSON.parse(JSON.stringify(extensionsRaw.value))
}

const eventEmitterListener = () => {
  initData()
}

onBeforeUnmount(() => {
  EventEmitter.removeListener('element-update', eventEmitterListener)
})

onMounted(() => {
  initData()
  EventEmitter.on('element-update', eventEmitterListener)
})
</script>

<template>
  <el-collapse-item name="extension">
    <template #title>
      <div class="collapse-header">
        <svg-icon icon="ri:add-circle-fill"></svg-icon>
        <h2>拓展属性</h2>
        <el-button type="primary" size="small" plain circle>{{ extensions.length }}</el-button>
      </div>
    </template>
    <el-form-item>
      <el-table :data="extensions" border>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="name" label="属性名" />
        <el-table-column prop="value" label="属性值" />
        <el-table-column prop="operate" label="操作" width="110" align="center">
          <template #default="scope">
            <el-button type="primary" icon="Edit" circle @click="edit(scope.row, scope.$index)"></el-button>
            <el-button type="danger" icon="Delete" circle @click="deleteRow(scope.$index)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button class="add" type="primary" icon="Plus" @click="add">添加属性</el-button>
    </el-form-item>
    <ExtensionPropertyAddEdit ref="ExtensionPropertyAddEditRef"></ExtensionPropertyAddEdit>
  </el-collapse-item>
</template>

<style scoped lang="scss">
.el-collapse-item {
  :deep(.el-collapse-item__header) {

    .collapse-header {
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0 12px;

      .el-icon {
        margin-right: 6px;
        font-size: 20px !important;
      }

      h2 {
        display: flex;
        flex: 1;
        justify-content: flex-start;
        font-weight: bold;
        font-size: 14px;
      }

      .el-button {
        margin-left: 12px;
      }
    }

    .el-icon {
      font-size: 18px;
    }
  }

  :deep(.el-collapse-item__content) {
    padding-right: 12px;
    padding-bottom: 12px;
    padding-left: 12px;
  }
}

.el-form-item {
  :deep(.el-form-item__content) {
    margin-left: 0 !important;

    .el-button.add {
      width: 100%;
      margin: 12px 0 0;
    }
  }
}
</style>
